package demo2;

import demo2.Person;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Yuekao01 {
    public static void main(String[] args)  {
        ArrayList<String> personList = new ArrayList<>();
        FileReader fileReader ;
        BufferedReader bufferedReader ;
        try {
            fileReader = new FileReader("D://a.txt");
            bufferedReader = new BufferedReader(fileReader);
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                personList.add(line); // 将每行文本添加到集合中
            }

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        // 打印集合中的文本内容
        for (String s : personList) {
            System.out.println(s);
        }

        //遍历读取每一行的内容
        for (String line:personList) {
            //赵分隔符
            String[] tokens = line.split(",");

        }
        Person person1 = new Person("xiaoming",10,"male",172,70);
        Person person2 = new Person("xiaobai",20,"male",177,80);
        Person person3 = new Person("xiaolv",21,"female",176,66);
        ArrayList<Person> list = new ArrayList<>();
        list.add(person1);
        list.add(person2);
        list.add(person3);

        // 删除未成年的数据
        list.removeIf(person -> person.getAge() < 18);

        // 计算平均年龄、最大年龄和最小年龄
        int totalAge = 0;
        int maxAge = Integer.MIN_VALUE;
        int minAge = Integer.MAX_VALUE;
        //使用集合遍历，获取平均值，最大值和最小值
        for (Person person : list) {
            totalAge += person.getAge();
            maxAge = Math.max(maxAge, person.getAge());
            minAge = Math.min(minAge, person.getAge());
        }
        double averageAge = (double) totalAge / personList.size();
        System.out.println("最大年龄："+minAge+"最小年龄："+maxAge+"平均年龄："+averageAge);
        // 按年龄和身高降序排序
        //将集合中的元素按照年龄进行降序排列，若年龄相仿，按照身高降序排列
        list.sort((p1,p2)->{
            int pp = p2.getAge() - p1.getAge();
            if(pp != 0){
                //如果年龄相减不为0，则说明年龄不相等，返回年龄降序排序
                return pp;
            }
            //如果年龄相等相减为0，则使用身高数据进行降序排序
            return (int) (p2.getHeight() - p1.getHeight());
        });
        list.forEach((System.out::println));




        // 打印结果
        System.out.println("删除未成年后的集合：");
        for (Person person : list) {
            System.out.println(person.getName() + ", " + person.getAge() + "岁, " + person.getGender() + ", " + person.getHeight() + "cm, " + person.getWeight() + "kg");
        }

        System.out.println("平均年龄: " + averageAge);
        System.out.println("最大年龄: " + maxAge);
        System.out.println("最小年龄: " + minAge);
    }



}